package com.feibi.hitutor

import android.os.Bundle
import android.os.Environment
import android.util.Log
import cn.jpush.android.api.JPushInterface
import com.imkit.hitutor.DemoApplication
import com.imkit.hitutor.FlutterCallback
import com.tencent.bugly.crashreport.CrashReport
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.io.File
import java.lang.Exception

class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        CrashReport.initCrashReport(applicationContext, "fda0207310", false)
        super.onCreate(savedInstanceState)
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        val messenger = flutterEngine.dartExecutor.binaryMessenger
        // 新建一个 Channel 对象
        val channel = MethodChannel(messenger, "com.feibi.chat/pin_chat")
        channel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
            val flutterCallback = FlutterCallback { success, msg ->
                if (success) result.success(true)
                else result.error("201", msg, null)
            }

            if ("configure" == call.method) {
                val clientKey = call.argument<String>("clientKey")
                val serverUrl = call.argument<String>("serverUrl")
                DemoApplication.configureIM(serverUrl, clientKey);
            } else if ("connect" == call.method) {
                val userId = call.argument<String>("userId")
                val accessToken = call.argument<String>("accessToken")
                DemoApplication.connect(this, userId ?: "", accessToken ?: "", flutterCallback)
            } else if ("updateMyProfile" == call.method) {
                val nickname = call.argument<String>("nickname")
                val avatarUrl = call.argument<String>("avatarUrl")
                val description = call.argument<String>("description")
                DemoApplication.updateUserInfo(this, nickname ?: "", avatarUrl ?: "", flutterCallback)
            } else if ("startChat" == call.method) {
                val toUserId = call.argument<String>("toUserId")
                DemoApplication.openChatPage(this, toUserId ?: "", flutterCallback)
            } else if ("goChatRoomList" == call.method) {
                DemoApplication.openRoomList(this)
            } else {
                result.error("404", "can not find this method", null)
            }
        }

        val pathChannel = MethodChannel(messenger, "PathUtil")
        pathChannel.setMethodCallHandler { call, result ->
            if ("getDownloadPath" == call.method) {
                val downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
                if (!File(downloadPath).exists()) {
                    File(downloadPath).mkdirs()
                }
                result.success(downloadPath);
            }
        }

        val jpushChannel = MethodChannel(messenger, "JPush")
        jpushChannel.setMethodCallHandler { call, result ->
            if ("init" == call.method) {
                JPushInterface.setDebugMode(true)
                JPushInterface.init(this)
                val regId: String = JPushInterface.getRegistrationID(this)
                result.success(regId);
            }
        }
    }

}
